package database;

import com.google.gson.JsonObject;
import com.mysql.jdbc.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

/**
 * Created by LeoLu on 2017/3/15.
 */
public class DBManager {
    private static DBManager manager;

    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/ktyplay_db";
    String dbName = "ktyplay_db";
    String username = "root";
    String password = "";
    private Connection conn;

    public static DBManager getInstance(){
        if (manager == null) {
            manager = new DBManager();
        }
        return manager;
    }

    private DBManager(){

    }

    public void openDB(){
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName +"?useUnicode=true&characterEncoding=utf-8&useSSL=false",username,password);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PreparedStatement getPreStatement(String sql){
        try {
            return (PreparedStatement) conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean isOpenDB()throws Exception{
        return !conn.isClosed();
    }


}
